/*****************************************************************************************
 * Aware : an Asynchonous Aware-Oriented Software Architecture
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *****************************************************************************************/
package dexter.aware.util;

import java.io.File;
import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;

import com.sun.management.OperatingSystemMXBean;

/**
 * Intrumentation static util class
 * 
 * @author Ruben Mondejar <rmondejara@uoc.edu>
 */
public class Instrumentation {
	
	private static OperatingSystemMXBean 
	mxbean = (OperatingSystemMXBean) ManagementFactory
	.getOperatingSystemMXBean();
		
	/**
	 * Returns the hostname
	 * @return hostname
	 * @throws UnknownHostException
	 */
	public static String getHostName() throws UnknownHostException {
		return InetAddress.getLocalHost().getHostName ();
	}
	
	/**
	 * Returns the free memory of the host
	 * @return freemem
	 */
	public static long getFreeMem() {

		// System.out.println("Runtime freeMem
		// :"+Runtime.getRuntime().freeMemory());

		long freeMem = mxbean.getFreePhysicalMemorySize()
				+ mxbean.getFreeSwapSpaceSize();
		return freeMem;

	}

	/**
	 * Returns the total memory of this host
	 * @return totalmem
	 */
	public static long getTotalMem() {

		// System.out.println("Runtime totalMem
		// :"+Runtime.getRuntime().totalMemory());

		long totalMem = mxbean.getTotalPhysicalMemorySize()
				+ mxbean.getTotalSwapSpaceSize();
		return totalMem;
	}

	/**
	 * Returns the free space on disk of this host
	 * @return freespace
	 */
	public static long getFreeSpace() {
		// Java 6 methods
		File f = new File(".");
		return f.getFreeSpace();
	}

	/**
	 * Returns the total space of the disk of this host
	 * @return totalspace
	 */
	public static long getTotalSpace() {
		// Java 6 methods
		File f = new File(".");
		return f.getTotalSpace();
	}
	
	/**
	 * Returns the total CPU work load average (sum of all cpus)
	 * @return load
	 */
	public static double getSystemLoadAverage() {
		// Java 6 method
		return mxbean.getSystemLoadAverage();
	}

	/**
	 * Returns the number of CPUs of this host
	 * @return cpus
	 */
	public static int getAvailableProcessors() {
		// Java 6 method
		return mxbean.getAvailableProcessors();
	}
//
//	public static long getProcessCpuTime() {
//		// Java 6 method
//		return mxbean.getProcessCpuTime();
//	}
//
//	public static int getNumOfThreads() {
//		// Java 6 method
//		ThreadMXBean threads = ManagementFactory.getThreadMXBean();
//		//return threads.getPeakThreadCount();
//		return threads.getThreadCount() - threads.getDaemonThreadCount();
//		//return threads.getThreadCount();	
//	}

}
